export default {
    namespace: 'todolist',
    state: [],
    reducers: {
        add(state, { payload: { value }}) {
            //用户点击 ‘添加’ 后，格式化数据并写入 state
            //标准数据格式: {id: 0, value: "测试", complete: false}
            const obj = {
                id: state.length,
                value,
                complete: false,
          };
            return [ ...state, obj];
        },
        change(state, { payload: { id }}) {
            state[id].complete = !state[id].complete;
            return [ ...state ]; // 浅修改可以简单地通过扩展运算符来促使 mapStateToProps 更新
            // 参考：https://github.com/camsong/redux-in-chinese/blob/master/docs/faq/ReactRedux.md
        },
        del(state, { payload: { id }}) {
            state.splice( id, 1);
            state.forEach( ( v, i ) => {
                v.id = i;   //更新 ID
            });
            return [ ...state ];
        },
    },
    effects: {
        // 该 demo 无需进行这类操作
    },
    subscriptions: {
        // 该 demo 无需监听数据源，所有的 Action 都由用户触发
    },
  };